package cc.pony.wordcralwer.util;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.methods.HttpGet;



public class WordInfoHelper {
	
	private final static String ICIBA_URL ="http://www.iciba.com/";
	
	
	
	WordInfo fetchWordFromICIBA(String wordname){		
		
		if(wordname==null || wordname.equals("")){
			return null;
		}
		WordInfo word = new WordInfo() ;
		

		
		HttpGet get = new HttpGet(ICIBA_URL+wordname);
		String string  =HttpHelper.getResponse(get);
		
		
		Pattern phoneticPattern = Pattern.compile("class=\"phonetic fontb font14\">(.*?)</span>");
		Matcher phoneticMatcher = phoneticPattern.matcher(string);
		if (phoneticMatcher.find()){
			 String phonetic = phoneticMatcher.group(1);
			 word.setPhonetic(phonetic);
			//System.out.println(phonetic);
		}
		  
		
	
		Pattern meanPattern = Pattern.compile("<span class=\"ec_mean\"><span class=\"show_num\">.*?</span>&nbsp;(.*?)</span>");
		Matcher meanMatcher = meanPattern.matcher(string);
		ArrayList<String> means = new ArrayList<String>();
		while(meanMatcher.find()){
			String mean = meanMatcher.group(1);
			means.add(mean);
			System.out.println(mean);
		}
		word.setChinese(means);
		
		word.setName(wordname);
		
		return word;
	}
	public static void main  (String[] args){
		WordInfoHelper impl= new WordInfoHelper();
		WordInfo word = impl.fetchWordFromICIBA("apple");
		System.out.println(word.getName());
		System.out.println(word.getPhonetic());
		System.out.println(word.getChinese().get(0));
		
	}
}
